{include file="common/head"}
<style>
    .hide {
        display: none;
    }
</style>
<form class="layui-form layui-padding-3 layui-form-pane" action="" lay-filter="goods">
    <div class="layui-form-item">
        <label class="layui-form-label">商品名称 *</label>
        <div class="layui-input-block">
            <input type="text" name="good_name" autocomplete="off" placeholder="请输入商品名称" lay-verify="required"
                class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品描述 *</label>
        <div class="layui-input-block">
            <textarea name="description" placeholder="请输入商品描述 " lay-verify="required" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品分类</label>
        <div class="layui-input-inline">
            <select name="cid" >
                {volist name="cateList" id="cate"}
                    <option value="{$cate.cid}">{$cate.cname}</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品类型</label>
        <div class="layui-input-inline">
            <select name="type" id="type">
                <option value="0">普通内容</option>
                <option value="1">卡密发送</option>
                <option value="2">VIP时长</option>
                <option value="3">会员组</option>
            </select>
        </div>
    </div>
   
    <div class="layui-form-item layui-form-text hide" id="good-content">
        <label class="layui-form-label ">商品内容 *</label>
        <div class="layui-input-block">
            <textarea name="content" placeholder="请输入商品内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item hide" id="vip_time">
        <label class="layui-form-label">会员时长</label>
        <div class=" layui-row">
            <div class="layui-col-xs4">
                原VIP时长：{$data.vip_time}
            </div>
            <div class="layui-col-xs4">
                <input type="text" name="vip_time_num" autocomplete="off" placeholder="请输入会员时长" class="layui-input">
            </div>
            <div class="layui-col-xs4">
                <select name="vip_time_type" id="">
                    <option value="1">天</option>
                    <option value="2">月</option>
                    <option value="3">年</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item hide" id="level">
        <label class="layui-form-label">会员组</label>
        <div class="layui-input-inline">
            <select name="level" id="level">
                <option value="0">青铜</option>
                <option value="1">白银</option>
                <option value="2">黄金</option>
                <option value="3">钻石</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品积分 *</label>
        <div class="layui-input-block">
            <input type="number" name="score" autocomplete="off" placeholder="请输入商品积分" lay-verify="required"
                class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">库存 *</label>
        <div class="layui-input-block">
            <input type="number" name="stock" autocomplete="off" placeholder="请输入商品库存" lay-verify="required"
                class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn layui-bg-blue" lay-submit lay-filter="submit">提交</button>
    </div>
</form>
{include file="common/bottom"}
<script>
    layui.use(function () {
        const layer = layui.layer
        const form = layui.form
        $ = layui.$
        let type = "{$data.type}"
        if(type == 0){
            $('#good-content').show()
        }else if(type == 2){
            $('#vip_time').show()
        }
        form.val('goods',{
            'good_name' : "{$data.good_name}"
            ,'cid' : "{$data.cid}"
            ,'description' : "{$data.description}"
            ,'content' : "{$data.content}"
            ,'type' : "{$data.type}"
            ,'level' : "{$data.level}"
            ,'score' : "{$data.score}"
            ,'stock' : "{$data.stock}"
        })
        form.on('submit(submit)', (data) => {
            let field = data.field
            let type = $('#type').val()
            if (type == 0 || type == 1 || type == 3) {
                delete field.vip_time_num
                delete field.level
                delete field.vip_time_type
            }
            field['id'] = "{$data.id}"
            $.post('/admin/goods/doUpdate', field, (res) => {
                if (res.code == 200) {
                    layer.msg('操作成功')
                } else {
                    layer.msg(res.message)

                }
            })
            return false
        })
       
        $('#type ~ .layui-form-select > .layui-anim-upbit > dd').click(() => {
            $('#vip_time').hide()
            $('#level').hide()
            $('#good-content').hide()
            setTimeout(() => {
                let type = $('#type').val()
                if (type == 0) {
                    $('#good-content').show()
                }
                else if (type == 2) {
                    $('#vip_time').show()
                } else if (type == 3) {
                    $('#level').show()
                }
            }, 200)
        })
    })

</script>